//
//  SelectPhoneCell.swift
//  MoyoungDevice
//
//  Created by 易先生 on 2022/9/5.
//

import UIKit
import EarbudsDatabase

class SelectPhoneCell: TableViewCell {

	override func makeUI() {
		super.makeUI()

		contentView.addSubview(headImage)
		contentView.addSubview(phoneLabel)
		contentView.addSubview(iconImage)

		headImage.snp.makeConstraints { make in
			make.leading.equalToSuperview().offset(20)
			make.width.equalTo(40)
			make.top.bottom.equalToSuperview()
		}

		iconImage.snp.makeConstraints { make in
			make.trailing.equalToSuperview().offset(-20)
			make.centerY.equalToSuperview()
			make.size.equalTo(CGSize(width: 20, height: 20))
		}

		phoneLabel.snp.makeConstraints { make in
            make.leading.equalTo(headImage.snp.trailing).offset(10)
			make.trailing.equalTo(iconImage.snp.leading).offset(-10)
			make.centerY.equalToSuperview()
		}
	}

	public func configImage(contact: ContactModel, image: UIImage) {
		self.headImage.image = image
		self.phoneLabel.text = contact.selectPhoneNumber
	}

	public func configSelect(currentSelectContact: [ContactModel], contact: ContactModel) {

		if currentSelectContact.first(where: { $0.phoneNumbers == contact.phoneNumbers && $0.fullName == contact.fullName && $0.selectPhoneNumber == contact.selectPhoneNumber }) != nil {
			self.iconImage.isHidden = false
		} else {
			self.iconImage.isHidden = true
		}
	}

	private lazy var headImage: ImageView = {
		let headImage = ImageView()
		headImage.contentMode = .scaleAspectFit
		headImage.layer.cornerRadius = 20
		headImage.layer.masksToBounds = true
		return headImage
	}()

	private lazy var phoneLabel: Label = {
		let phoneLabel = Label()
        phoneLabel.textColor = R.color.common_23_text_second()
        phoneLabel.font = Fonts.systemFont(ofSize: 14)
		phoneLabel.numberOfLines = 0
		return phoneLabel
	}()

	private lazy var iconImage: UIImageView = {
		let iconImage = UIImageView()
        iconImage.image = R.image.ic_common_pick()
		return iconImage
	}()

	override func setSelected(_ selected: Bool, animated: Bool) {
		super.setSelected(selected, animated: animated)

		// Configure the view for the selected state
	}

}
